<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="formRules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="(item) in list" :key="item.id" :value="item.username" :label="item.username" />

      </el-select></el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addDepartment, departmentInfo, changedepartment } from '@/api/department'
import { getEmployees } from '@/api/employees'
export default {
  props: {
    curId: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    oringinList: {
      type: Array,
      required: true
    }
  },
  data() {
    // code自定义
    const codeVailda = (rules, value, callback) => {
      let codeList = this.oringinList.map(item => item.code)
      if (this.isEdit) {
        codeList = this.oringinList.filter(item => item.id !== this.curId).map(item => item.code)
      }
      codeList.includes(value) ? callback(new Error('编码' + value + '已经存在')) : callback()
    }
    // 部门名自定义
    const nameValida = (rules, value, callback) => {
      let nameList = this.oringinList.filter(item => item.pid === this.curId).map(item => item.name)
      if (this.isEdit) {
        const curitem = this.oringinList.find(item => item.id === this.curId)
        const pid = curitem.pid
        nameList = this.oringinList.filter(item => item.pid === pid && item.id !== this.curId).map(item => item.name)
      }
      nameList.includes(value) ? callback(new Error('名字' + value + '已经存在')) : callback()
    }
    return {
      // 简单员工列表
      list: [],
      // 表单数据
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      formRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { pattern: /^\S{1,50}$/, message: '必须是1-50位的非空字符串', trigger: 'blur' },
          { validator: nameValida, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编号', trigger: 'blur' },
          { pattern: /^\S{1,50}$/, message: '必须是1-50位的非空字符串', trigger: 'blur' },
          { validator: codeVailda, trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { pattern: /^\S{1,300}$/, message: '必须是1-300位的非空字符串', trigger: 'blur' }
        ],
        manager: [{ required: true, trigger: 'blur' }]
      }
    }
  },
  created() {
    // 调用获取员工简单列表
    this.setEmployees()
    if (this.isEdit) {
      // 调用根据ID查询部门详情
      this.getdepartmentInfo()
    }
  },
  methods: {
    // 新增部门
    async  putDepartment() {
      try {
        await addDepartment({ ...this.form, pid: this.curId })
        // console.log(res, '新增部门')
        this.$message.success('新增成功')
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取员工简单列表
    async setEmployees() {
      try {
        const { data: res } = await getEmployees()
        //   console.log(res, '员工简单列表')
        this.list = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 确定
    hSubmit() {
      // 兜底校验
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return // 如果表单验证不通过，直接return
        // 通过的代码
        // 调用新增部门
        this.isEdit ? this.dochangedepartment() : this.putDepartment()
      })
    },
    // 取消
    hCancel() {
      this.$emit('Cancel')
    },
    // 根据ID查询部门详情
    async getdepartmentInfo() {
      try {
        const { data: res } = await departmentInfo(this.curId)
        //   console.log(res)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 根据ID修改部门详情
    async dochangedepartment() {
      try {
        await changedepartment(this.form)
        //   console.log(res, '根据ID修改部门详情')
        this.$emit('success')
        this.$message.success('修改成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
